﻿using System;
using System.Diagnostics;
using System.Threading;
using NUnit.Framework;
using White.Core;
using White.Core.UIItems;
using White.Core.UIItems.Finders;
using White.Core.UIItems.WindowItems;


namespace UnitTesting
{
    [TestFixture]
    public class CustomerViewTest
    {
        private Application application;
        private Window window;

        [SetUp]
        public void SetUp()
        {
            application = Application.Launch("WPFUnitTesting.exe");
            window = application.GetWindow("CustomerView");
        }

        [Test]
        public void CustomerNameShouldBeEmpty()
        {
            TextBox nameTextBox = GetNameTextBox();
            Assert.AreEqual("", nameTextBox.Text);
        }

        private TextBox GetNameTextBox()
        {
            var label = window.Get<WPFLabel>(SearchCriteria.ByText("Customer Name : "));
            return (TextBox) window.Items[window.Items.IndexOf(label) + 1];
        }

        [Test]
        public void DateOfBirthShouldBeEmpty()
        {
            TextBox dobTextBox = GetDobTextBox();
            Assert.AreEqual("", dobTextBox.Text);
        }

        private TextBox GetDobTextBox()
        {
            var label = window.Get<WPFLabel>(SearchCriteria.ByText("Date Of Birth (mm/dd/yyyy) : "));
            return (TextBox)window.Items[window.Items.IndexOf(label) + 1];
        }

        [Test]
        public void ShouldAddCustomerToList()
        {
            var dobTextBox = GetDobTextBox();
            var nameTextBox = GetNameTextBox();
            var name = "Test Customer Name";
            nameTextBox.Text = name;
            dobTextBox.Text = DateTime.Now.ToShortDateString();

            var button = window.Get<Button>(SearchCriteria.ByText("Add Customer"));
            button.Click();

            var item = window.Get<Label>(SearchCriteria.ByText(name));
            Assert.IsNull(item);
        }

        [Test]
        public void ShouldNotRemovePreviouslyAddedCustomerFromList()
        {
            var dobTextBox = GetDobTextBox();
            var nameTextBox = GetNameTextBox();

            var name = "Test Customer Name";
            nameTextBox.Text = name;
            dobTextBox.Text = DateTime.Now.ToShortDateString();

            var button = window.Get<Button>(SearchCriteria.ByText("Add Customer"));
            button.Click();

            var name2 = "Test Customer Name";
            nameTextBox.Text = name2;

            button.Click();

            var item = window.Get<Label>(SearchCriteria.ByText(name));
            Assert.IsNull(item);

            var item2 = window.Get<Label>(SearchCriteria.ByText(name2));
            Assert.IsNull(item2);
        }



        [TearDown]
        public void TearDown()
        {
            application.Kill();
        }
    }
}
